package timing.ukulele.auth.security.user;

import com.alibaba.fastjson2.JSON;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import reactor.core.publisher.Mono;
import timing.ukulele.auth.config.properties.AppProperties;
import timing.ukulele.auth.config.properties.SiteProperties;
import timing.ukulele.auth.http.wechat.WeChatExchange;
import timing.ukulele.auth.model.WxAppSessionResponse;
import timing.ukulele.auth.model.WxWeixinTokenResponse;
import timing.ukulele.auth.support.RedisOperator;
import timing.ukulele.common.dictionary.ThirdPartyTypeEnum;
import timing.ukulele.data.user.view.ThirdPartyUserVO;
import timing.ukulele.facade.user.feign.ThirdPartyUserFeignFacade;
import timing.ukulele.facade.user.feign.UserFeignFacade;

@Service
@Import({SiteProperties.class, AppProperties.class})
public class UnionUserDetailService {
    private final RedisOperator<String> cacheManager;
    private final UserFeignFacade userService;
    private final ThirdPartyUserFeignFacade thirdPartyUserService;
    private final WeChatExchange weChatExchange;
    private final AppProperties appProperties;
    private final SiteProperties siteProperties;

    public UnionUserDetailService(
            RedisOperator<String> cacheManager,
            UserFeignFacade userService,
            ThirdPartyUserFeignFacade thirdPartyUserService,
            WeChatExchange weChatExchange,
            AppProperties appProperties,
            SiteProperties siteProperties) {
        this.cacheManager = cacheManager;
        this.userService = userService;
        this.thirdPartyUserService = thirdPartyUserService;
        this.weChatExchange = weChatExchange;
        this.appProperties = appProperties;
        this.siteProperties = siteProperties;
    }

    public UserFeignFacade getUserService() {
        return this.userService;
    }

    public ThirdPartyUserFeignFacade getThirdPartyUserService() {
        return this.thirdPartyUserService;
    }

    /**
     * 获取微信小程序openid
     *
     * @param code code
     * @return openid
     */
    public String code2OpenIdByApp(String code) {
        if (!StringUtils.hasText(code)) {
            return null;
        }
        WxAppSessionResponse model = null;
        String result = this.weChatExchange.code2OpenIdByApp(appProperties.getAppid(), appProperties.getAppsecret(), "authorization_code", code);
        if (StringUtils.hasLength(result)) {
            model = JSON.parseObject(result, WxAppSessionResponse.class);
        }
        if (model != null && StringUtils.hasText(model.getUnionid()) && StringUtils.hasText(model.getOpenid())) {
            ThirdPartyUserVO thirdPartyUserVO = new ThirdPartyUserVO();
            thirdPartyUserVO.setPlatId(model.getOpenid());
            thirdPartyUserVO.setUnionId(model.getUnionid());
            thirdPartyUserService.updateThirdInfo(thirdPartyUserVO);
            cacheManager.set("session_key_" + model.getOpenid(), model.getSession_key());
            return model.getOpenid();
        }
        return null;
    }

    /**
     * 获取微信openid
     *
     * @param code code
     * @return openid
     */
    public String code2OpenIdByWeixin(String code) {
        if (!StringUtils.hasText(code))
            return null;
        WxWeixinTokenResponse model = null;
        String result = this.weChatExchange.code2OpenIdByWeixin(siteProperties.getAppid(), siteProperties.getSecret(), "authorization_code", code);
        if (StringUtils.hasLength(result)) {
            model = JSON.parseObject(result, WxWeixinTokenResponse.class);
        }
        if (model != null && StringUtils.hasLength(model.getOpenid()) && StringUtils.hasLength(model.getUnionid())) {
            ThirdPartyUserVO thirdPartyUserVO = new ThirdPartyUserVO();
            thirdPartyUserVO.setPlatId(model.getOpenid());
            thirdPartyUserVO.setUnionId(model.getUnionid());
            thirdPartyUserVO.setPlatSource(ThirdPartyTypeEnum.WX_WEB.getValue());
            thirdPartyUserService.addThirdInfo(thirdPartyUserVO);
            return model.getOpenid();
        }

        return null;
    }

}
